/*
 * Copyright 2008 Jens Meiss
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License. 
 * You may obtain a copy of the License at 
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package mtt.appinfo;

import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Form;

import mtt.appinfo.events.ShowApplicationInfoViewEvent;
import mtt.menu.events.ShowMainMenuEvent;
import mtt.rms.recordstores.calendar.events.GetNumCalendarRecordsEvent;
import mtt.rms.recordstores.calendar.events.SetNumCalendarRecordsEvent;
import mtt.rms.recordstores.marks.events.GetNumMarkRecordsEvent;
import mtt.rms.recordstores.marks.events.SetNumMarkRecordsEvent;
import hmvc.j2me.controller.AController;
import hmvc.j2me.events.IEvent;
import hmvc.j2me.events.ShowDisplayAbleEvent;
import hmvc.j2me.events.TerminateEvent;

/**
 * 
 * 
 * @author jens.meiss
 */
public class ApplicationInfoController extends AController implements
		CommandListener {

	private ApplicationInfoView view;

	public ApplicationInfoController() {
		
		setView(new ApplicationInfoView());
		
	}
	
	public void commandAction(Command arg0, Displayable arg1) {

			fireEventUp(new ShowMainMenuEvent());
			fireEvent(new TerminateEvent(), false, true, false);
	}

	private ApplicationInfoView getView() {
		return view;
	}

	public void init() {
		
		Form form = new Form("MTT - Informationen");
		form.setCommandListener(this);
		getView().setForm(form);
		
		Command commandBack = new Command("zurück", Command.BACK, 0);

		form.append("Author: Jens Meiß");
		form.append("Version: 0");
		form.append("Homepage: http://mobile-time-table.googlecode.com");
		form.append("Lizenz: Apache 2.0 (OpenSource)");

		form.addCommand(commandBack);

		
		register(ShowApplicationInfoViewEvent.class);
		register(SetNumCalendarRecordsEvent.class);
		register(SetNumMarkRecordsEvent.class);
		
		fireEventUp(new GetNumCalendarRecordsEvent(this));
		fireEventUp(new GetNumMarkRecordsEvent(this));
	}

	public void receiveEvent(IEvent event) {
		
		if (event instanceof ShowApplicationInfoViewEvent) {
			
			fireEventUp(new ShowDisplayAbleEvent(getView().getDisplayable()));
			
		} else if (event instanceof SetNumCalendarRecordsEvent) {
			
			SetNumCalendarRecordsEvent numCalendarRecordsEvent = (SetNumCalendarRecordsEvent) event;
			getView().getForm().append("Gespeicherte Termine: "
					+ numCalendarRecordsEvent.getNumCalendarRecords());
		} else if (event instanceof SetNumMarkRecordsEvent) {
			
			SetNumMarkRecordsEvent numMarksRecordStoreEvent = (SetNumMarkRecordsEvent) event;
			getView().getForm().append("Gespeicherte Noten/Punkte: "
					+ numMarksRecordStoreEvent.getNumMarksRecords());
		}
	}

	private void setView(ApplicationInfoView view) {
		this.view = view;
	}

	public void terminate() {
	}
}
